package com.ruoyi.account.service.impl;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.account.enums.AccountTypeEnum;
import com.ruoyi.account.enums.ActionTypeEnum;
import com.ruoyi.account.enums.RecordStatusEnum;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.db.domain.*;
import com.ruoyi.db.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.account.service.IAccountApplyRecordService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 账户入账申请Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-02-02
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class AccountApplyRecordServiceImpl implements IAccountApplyRecordService 
{
    @Resource
    private AccountApplyRecordMapper accountApplyRecordMapper;
    @Resource
    private AccountInfoMapper accountInfoMapper;
    @Resource
    private AccountItemChannleMapper accountItemChannleMapper;
    @Resource
    private AccountItemProductMapper accountItemProductMapper;
    @Resource
    private AccountItemSpecialMapper accountItemSpecialMapper;
    @Resource
    private BillInfoProductMapper billInfoProductMapper;
    @Resource
    private BillInfoChannelMapper billInfoChannelMapper;

    /**
     * 查询账户入账申请
     * 
     * @param id 账户入账申请主键
     * @return 账户入账申请
     */
    @Override
    public AccountApplyRecord selectAccountApplyRecordById(Long id)
    {
        return accountApplyRecordMapper.selectAccountApplyRecordById(id);
    }

    /**
     * 查询账户入账申请列表
     * 
     * @param accountApplyRecord 账户入账申请
     * @return 账户入账申请
     */
    @Override
    public List<AccountApplyRecord> selectAccountApplyRecordList(AccountApplyRecord accountApplyRecord)
    {
        return accountApplyRecordMapper.selectAccountApplyRecordList(accountApplyRecord);
    }

    /**
     * 查询账户入账申请列表
     *
     * @param accountApplyRecord 账户入账申请
     * @return 账户入账申请
     */
    @Override
    public List<Map<String, Object>> selectAccountApplyRecordListMap(AccountApplyRecord accountApplyRecord)
    {
        return accountApplyRecordMapper.selectAccountApplyRecordListMap(accountApplyRecord);
    }

    /**
     * 新增账户入账申请
     * 
     * @param accountApplyRecord 账户入账申请
     * @return 结果
     */
    @Override
    public AjaxResult insertAccountApplyRecord(AccountApplyRecord accountApplyRecord)
    {

        String dateInfo = LocalDate.now().toString();
        accountApplyRecord.setDateInfo(dateInfo);

        // 根据操作类型，判断缺少的值
        if (ActionTypeEnum.needMainInfoList.contains(accountApplyRecord.getActionType())){
            if (ObjectUtil.isEmpty(accountApplyRecord.getMainAccountId())){
                return AjaxResult.error("请选择总账户信息");
            }
        }

        // 总账户信息
        if (ObjectUtil.isNotEmpty(accountApplyRecord.getMainAccountId()) && !StrUtil.equals("0", accountApplyRecord.getMainAccountId())) {
            AccountInfo accountInfo = accountInfoMapper.selectAccountInfoById(Long.valueOf(accountApplyRecord.getMainAccountId()));
            if (ObjectUtil.isNotEmpty(accountInfo)) {
                accountApplyRecord.setMainAccountName(accountInfo.getName());
            }
        }

        if (StrUtil.equals(AccountTypeEnum.CHAN.getValue(), accountApplyRecord.getItemAccountType())){
            AccountItemChannle accountItemChannle =
                    accountItemChannleMapper.selectAccountItemChannleById(Long.valueOf(accountApplyRecord.getItemAccountId()));
            accountApplyRecord.setItemAccountName(accountItemChannle.getAccountName());
        }else if (StrUtil.equals(AccountTypeEnum.PROD.getValue(), accountApplyRecord.getItemAccountType())){
            AccountItemProduct accountItemProduct =
                    accountItemProductMapper.selectAccountItemProductById(Long.valueOf(accountApplyRecord.getItemAccountId()));
            accountApplyRecord.setItemAccountName(accountItemProduct.getAccountName());
        }else if (StrUtil.equals(AccountTypeEnum.SPECIAL.getValue(), accountApplyRecord.getItemAccountType())){
            AccountItemSpecial accountItemSpecial =
                    accountItemSpecialMapper.selectAccountItemSpecialById(accountApplyRecord.getItemAccountId());
            accountApplyRecord.setItemAccountName(accountItemSpecial.getAccountName());
        }else if (StrUtil.equals(AccountTypeEnum.ACCOUNT.getValue(), accountApplyRecord.getItemAccountType())){
            AccountInfo accountInfo =
                    accountInfoMapper.selectAccountInfoById(Long.valueOf(accountApplyRecord.getItemAccountId()));
            accountApplyRecord.setItemAccountName(accountInfo.getName());
        }

        int i = accountApplyRecordMapper.insertAccountApplyRecord(accountApplyRecord);
        if (i > 0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 修改账户入账申请
     * 
     * @param accountApplyRecord 账户入账申请
     * @return 结果
     */
    @Override
    public int updateAccountApplyRecord(AccountApplyRecord accountApplyRecord)
    {

        if (ObjectUtil.isNotEmpty(accountApplyRecord.getMainAccountId())){
            AccountInfo accountInfo = accountInfoMapper.selectAccountInfoById(Long.valueOf(accountApplyRecord.getMainAccountId()));
            if (ObjectUtil.isNotEmpty(accountInfo)){
                accountApplyRecord.setMainAccountName(accountInfo.getName());
            }
        }

        return accountApplyRecordMapper.updateAccountApplyRecord(accountApplyRecord);
    }

    /**
     * 批量删除账户入账申请
     * 
     * @return 结果
     */
    @Override
    public int deleteAccountApplyRecordByIds(AccountApplyRecord accountApplyRecord)
    {

        // 查询是否可以删除
        AccountApplyRecord accountApplyRecord1 = accountApplyRecordMapper.selectAccountApplyRecordById(accountApplyRecord.getId());
        if (!ObjectUtil.equal(accountApplyRecord1.getStatus(), RecordStatusEnum.WAIT_CONFIRM.getValue())){
            // 只有待确认状态才可以删除
            return -1;
        }

        accountApplyRecord.setStatus(RecordStatusEnum.UNABLE.getValue());
        accountApplyRecord.setDelFlag(Long.valueOf(1));
        accountApplyRecord.setUpdateTime(DateUtils.getNowDate());
        return accountApplyRecordMapper.updateAccountApplyRecord(accountApplyRecord);
    }

    /**
     * 删除账户入账申请信息
     * 
     * @param id 账户入账申请主键
     * @return 结果
     */
    @Override
    public int deleteAccountApplyRecordById(Long id)
    {
        return accountApplyRecordMapper.deleteAccountApplyRecordById(id);
    }

    /**
     * 拒绝入账
     * @param recordId
     * @return
     */
    @Override
    public int refuseEnter(String recordId) {

        // 直接写操作逻辑
        AccountApplyRecord updateEntity = new AccountApplyRecord();
        updateEntity.setUpdateTime(new Date());
        updateEntity.setId(Long.valueOf(recordId));
        updateEntity.setStatus(RecordStatusEnum.WAIT_CONFIRM.getValue());

        // 如果是消耗类型的话，设置为删除状态
        return accountApplyRecordMapper.updateAccountApplyRecord(updateEntity);
    }
}
